<template>
	<view class="container">
		<view class="form-box">
			<view class="form">
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">收货人</view>
						<input class="input" placeholder="请输入收货人" :maxlength="10" v-model="address.name" placeholder-class="text-color-assist" />
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">性别</view>
						<view class="radio-group">
							<view class="radio" :class="{ checked: address.sex == 1 }" style="margin-right: 10rpx" @tap="address.sex = 1">先生</view>
							<view class="radio" :class="{ checked: address.sex == 2 }" @tap="address.sex = 2">女士</view>
						</view>
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">联系方式</view>
						<input class="input" :maxlength="11" placeholder="请输入收货人联系方式" v-model="address.phone" placeholder-class="text-color-assist" />
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input" @click.stop="getAddress">
						<view class="label">收货地址</view>
						<input class="input" placeholder="请选择收货地址" disabled :maxlength="80" v-model="address.address" placeholder-class="text-color-assist" />
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">详细地址</view>
						<input class="input" placeholder="请输入详细地址" :maxlength="255" v-model="address.location" placeholder-class="text-color-assist" />
					</view>
				</list-cell>
			</view>
			<view class="btn-section">
				<view class="btn" @tap="save">保存</view>
			</view>
			<uni-popup ref="dialogLocation" type="dialog">
				<uni-popup-dialog type="info" cancelText="取消" confirmText="同意" title="温馨提示" content="请授权位置权限方便送货" @confirm="ok"></uni-popup-dialog>
			</uni-popup>
		</view>
	</view>
</template>

<script lang="ts" setup>
import { onUnmounted, ref,onMounted } from 'vue';
import { onShow } from '@dcloudio/uni-app';
import listCell from '../components/list-cell/list-cell';
import Address, { addressInit } from '../../vendor/address/Address';
import { state } from '../../store/user';
import { state as globalState, setAddress, setLongitude, setLatitude } from '../../store/global';
const { add, address, update } = Address();
const dialogLocation = ref();
let t;

if (state.editAddress && state.editAddress.id) {
	address.value = JSON.parse(JSON.stringify(state.editAddress));
}
onShow(() => {
	address.value.address = globalState.address?globalState.address:address.value.address;
	address.value.longitude = globalState.longitude;
	address.value.latitude = globalState.latitude;

});

onMounted(()=>{
	clearTimeout(t)
})

const save = () => {
	if (address.value.id) {
		// 修改
		update(() => {
			uni.showToast({
				title: '修改成功',
				icon: 'success'
			});

			state.addresses.some((item) => {
				if (item.id == address.value.id) {
					item.sex = address.value.sex;
					item.name = address.value.name;
					item.phone = address.value.phone;
					item.address = address.value.address;
					item.location = address.value.location;
					item.longitude = address.value.longitude;
					item.latitude = address.value.latitude;
					return true;
				}
			});

			t = setTimeout(function () {
				uni.navigateBack();
			}, 1500);
		});
	} else {
		// 新增
		add((res: any) => {
			if (res.id) {
				uni.showToast({
					title: '添加成功',
					icon: 'success'
				});

				address.value.id = res.id;

				state.addresses.push(address.value);
				
				address.value = addressInit

				t = setTimeout(function () {
					uni.navigateBack();
				}, 1500);
			} else {
				uni.showToast({
					title: '添加失败',
					icon: 'error'
				});
			}
		});
	}
};

onUnmounted(() => {
	// 重置地址
	setAddress('');
	setLongitude('');
	setLatitude('');
});

const getAddress = () => {
	console.log('地图');
	// 获取位置
	// #ifdef MP-WEIXIN
	uni.getSetting({
		success(res) {
			if (res.authSetting['scope.userLocation']) {
				// 地理位置
				uni.navigateTo({
					url: '/pages/map'
				});
			} else {
				// 获取位置授权
				dialogLocation.value.open();
			}
		}
	});
	// #endif

	// #ifdef H5

	uni.navigateTo({
		url: '/pages/map'
	});
	// #endif
};

const ok = () => {
	uni.openSetting({
		success(res) {
			// 返回授权结果
			if (res.authSetting['scope.userLocation']) {
				// 已授权位置权限
				getAddress();
			}
		}
	});
};
</script>

<style lang="scss" scoped>
.form-box {
	width: 100%;
	height: 100%;
	padding: 30rpx;
	display: flex;
	flex-direction: column;
	color: $text-color-base;

	.form-input {
		display: flex;
		align-items: center;
		width: 100%;
	}

	.label {
		width: 200rpx;
		font-size: $font-size-lg;
		color: $text-color-base;
		font-weight: 500;
	}

	.input {
		flex: 1;
		display: flex;
		align-items: center;
	}

	.radio-group {
		display: flex;
		justify-content: flex-start;

		.radio {
			padding: 10rpx 30rpx;
			border-radius: 6rpx;
			border: 2rpx solid $text-color-assist;
			color: $text-color-assist;
			font-size: $font-size-base;

			&.checked {
				background-color: $color-primary;
				color: $text-color-white;
				border: 2rpx solid $color-primary;
			}
		}
	}

	.btn-section {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;

		button {
			font-size: $font-size-base;
			height: 90rpx;
			border-radius: 50rem !important;
			width: 85%;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
}
.btn {
	text-align: center;
	color: white;
	line-height: 50px;
	height: 50px;
	width: 80%;
	margin: 30px auto;
	border-radius: 30px;
	background: #a5d63f;
}
</style>
